import wsgiref.handlers
from google.appengine.ext import webapp


from pixeltoy import model


class GetSnapshotImage(webapp.RequestHandler):
  def get(self):
    try:
      id = int(self.request.get('id'))
    except ValueError:
      return

    snapshot = model.Snapshot.get_by_id(ids=id)
    if snapshot is None:
      return

    self.response.headers['Content-Type'] = 'image/png'
    self.response.out.write(snapshot.data)


def main():
  application = webapp.WSGIApplication([('/snapshot', GetSnapshotImage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == "__main__":
  main()
